import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/views/Dynamic/DynamicList.dart';
import 'package:thinking/widget/_.dart';


class Dynamic extends StatefulWidget{

  @override
  _Dynamic createState() => _Dynamic();

}


class _Dynamic extends State<Dynamic> with  AutomaticKeepAliveClientMixin<Dynamic>,SingleTickerProviderStateMixin{

  @override
  bool get wantKeepAlive => true;
  late TabController _tabController;
  late PageController _pageController;
  List<String> tabs =['综合','关注'];

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 2, vsync: this);
    _pageController = PageController();
  }

  @override
  void dispose() {
    _tabController.dispose();
    _pageController.dispose();
    super.dispose();
  }

  Widget title(){

    return Container(
      child: TabBar(
        controller: _tabController,
        tabs: tabs.map((e) =>
            Padding(
              padding: EdgeInsets.only(top: 6,bottom: 10),
              child:Text(e) ,
            )
        ).toList(),
        isScrollable: true,
        labelColor: ColorConfig.ThemeObsColor,
        labelStyle:TextStyle(fontSize: 18,fontWeight: FontWeight.w600),
        unselectedLabelColor: ColorConfig.NoActiveColor,
        unselectedLabelStyle:TextStyle(fontSize: 16) ,
        indicatorWeight:0.1,
        onTap: (int i) {
          _pageController.jumpToPage(i);
        },
      ),
    );

  }

  PreferredSizeWidget bottom(){

    return PreferredSize(
      preferredSize: Size.fromHeight(36) ,
      child: Panel(
        top: 0,
        left: 20,
        right: 20,
        bottom: Screen.setHeight(7),
        child: GestureDetector(
          child: ShowSearchWidget(
            backgroundColor: ColorConfig.LineColor,
            textColor: ColorConfig.TextColor,
            textVal: '搜索动态',
          ),
          onTap: (){
            Modular.to.pushNamed('/indexSearch/1');
          },
        ),
      )

    );

  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
          appBar: AppBar(
            centerTitle: true,
            backgroundColor: ColorConfig.WhiteBackColor,
            toolbarHeight: Screen.setHeight(42),
            elevation: 2,
            title: title(),
            bottom: bottom(),
          ),
        floatingActionButton: FloatingActionButton(
          onPressed: (){
            Modular.to.pushNamed('/dynamicNews');
          },
          backgroundColor: ColorConfig.ThemeColor,
          child:Icon(
            Icons.add,
            size: 25,
            color: ColorConfig.WhiteBackColor,
          ) ,
        ),
          body: PageView(
            controller: _pageController,
            children: tabs.asMap().keys.map((e) =>
                Container(
                  child: DynamicList(index:e),
                )
            ).toList(),
            onPageChanged: (e){
              _tabController.animateTo(e);
            },
          ) ,

    );

  }

}